<?php

namespace App\Http\Controllers\Api;

use App\ApiSubjectOne;
use App\Cate;
use App\Chapter;
use App\Mp3;
use App\Problem;
use App\SubjectOne;
use Illuminate\Http\Request;


class SuboneController extends Controller
{

    /**
     * 易错题
     * @return int
     */
    public function GetFallibleQuestions()
    {

        $model = SubjectOne::
            where('is_yct',1)
            ->with('image')
            ->with('chapters')
            ->with('cate')
            ->get();

        foreach ($model as $k=>$v){
            $model[$k]['url'] = $v['image']['url'];
            $model[$k]['chapter'] = "";
        }

        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$model,
        ]);
    }


    /**
     *  争议题
     * @return int
     */
    public function TopicOfDispute()
    {
        $model = SubjectOne::where('is_zy',1)
            ->with('image')
            ->with('chapters')
            ->with('cate')
            ->get();

        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$model,
        ]);
    }


    /**
     * 分类列表
     * @param Request $request
     * @return array|null|string
     */
    public function Classification(Request $request)
    {

        $type= $request->input('type');


        //查找对应的分类数据
        $list =SubjectOne::with('image')
            ->with('chapters')
            ->where('cate_id',$type)
            ->orWhere('cate_id','like','%'.$type.',%')
            ->orWhere('cate_id','like','%,'.$type.'%')
            ->get();


        foreach ($list as $k=>$v){
            $list[$k]['url'] = $v['image']['url'];
            $list[$k]['chapter'] = "";
        }

        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$list,
        ]);


    }


    /**
     * 顺序练习
     * @return mixed
     */
    public function SequencePractice()
    {


        $list = ApiSubjectOne::with(['image','chapters'])
            ->get();

        foreach ($list as $k=>$v){
           $list[$k]['url'] = $v['image']['url'];
           $list[$k]['chapter'] = $v['chapters']['name'];
        }


        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$list,
        ]);
    }

    /**
     * 顺序练习
     * @return mixed
     */
    public function RandomPactice()
    {

        $list = ApiSubjectOne::with(['image','chapters'])
            ->inRandomOrder()
            ->get();

        foreach ($list as $k=>$v){
            $list[$k]['url'] = $v['image']['url'];
            $list[$k]['chapter'] = $v['chapters']['name'];
        }

        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$list,
        ]);

//        $list = ApiSubjectOne::with(['image','chapters'])
//            ->inRandomOrder()
//            ->get();
//
//        foreach ($list as $k=>$v){
//           $list[$k]['url'] = $v['image']['url'];
//           $list[$k]['chapter'] = '';
//        }


//        return $this->response->array([
//            'code'=>200,
//            'message'=>'ok',
//            'data'=>$list,
//        ]);
    }


    /**
     * 科目一分类
     * @return mixed
     */
    public function GetSubOneCate()
    {
        $list = Cate::where('cate',1)->orderBy('index','asc')->limit(10)->get();
        foreach ($list as $k=>$v){
            $num = SubjectOne::where('cate_id',$v['id'])
                ->orWhere('cate_id','like','%,'.$v['id'].'%')
                ->orWhere('cate_id','like','%'.$v['id'].',%')
                ->count();
            $list[$k]['num'] = $num;
        }
        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$list,
        ]);
    }


    /**
     * 章节列表
     * @return mixed
     */
    public function GetChaptersList()
    {
        $list = Chapter::where('cate',1)->get();
        foreach ($list as $k=>$v){
            $num = SubjectOne::where('chapters_id',$v['id'])
                ->count();
            $list[$k]['num'] = $num;
        }
        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$list,
        ]);
    }


    /**
     * 仿真练习
     * @return int
     */
    public function SimulationPractice()
    {

        $list1 = ApiSubjectOne::with(['image','chapters'])
            ->where('chapters_id',1)
            ->inRandomOrder()
            ->take(40)
            ->get();
        $list2 = ApiSubjectOne::with(['image','chapters'])
            ->where('chapters_id',2)
            ->inRandomOrder()
            ->take(30)
            ->get();

        $list3 = ApiSubjectOne::with(['image','chapters'])
            ->where('chapters_id',3)
            ->inRandomOrder()
            ->take(20)
            ->get();

        $list4 = ApiSubjectOne::with(['image','chapters'])
            ->where('chapters_id',11)
            ->inRandomOrder()
            ->take(10)
            ->get();



        foreach ($list1 as $k=>$v){
            $list[] = $v;
            $list[$k]['url'] = $v['image']['url'];
            $list[$k]['chapter'] = '';
        }


        foreach ($list2 as $k=>$v){
            $list[] = $v;
            $list[40+$k]['url'] = $v['image']['url'];
            $list[40+$k]['chapter'] = '';
        }


        foreach ($list3 as $k=>$v){
            $list[] = $v;
            $list[70+$k]['url'] = $v['image']['url'];
            $list[70+$k]['chapter'] = '';
        }


        foreach ($list4 as $k=>$v){
            $list[] = $v;
            $list[90+$k]['url'] = $v['image']['url'];
            $list[90+$k]['chapter'] = '';
        }


        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$list,
        ]);
    }


    /**
     * 常见问题
     * @return mixed
     * @throws \ErrorException
     */
    public function GetProblemList(Request $request)
    {
        $type = $request->type==''?1:$request->type;
        $list = Problem::where('type',$type)->limit(4)->get();
        $list = $list->each(function ($v,$k){
            $v['content'] = '/problem/'.$v['id'];
        });
        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$list,
        ]);
    }


    public function Mp3List()
    {
        $list = Mp3::with('image')->get();
        $list = $list->each(function ($item, $key) {
            $item['images'] = $item['image']['url'];
            unset($item['image']);
        });
        return $this->response->array([
            'code'=>200,
            'message'=>'ok',
            'data'=>$list,
        ]);
    }
}
